# -*- coding: utf-8 -*-
from Cheetah.Template import Template
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from pymongo import Connection
import commonlib
import datetime
import mimetypes
import os
import pickle
import pprint
import smtplib
import sys
import traceback

MONGO_SERVER = 'beta.mana.vn'
MONGO_PORT = 27017
MAX_ACTION = 30
gmail_user = "monitor@mana.vn"
gmail_pwd = "monitor@)!!"
logger = commonlib.getMongoLog(MONGO_SERVER, MONGO_PORT, 'crawler_mornitor')
    
def mail(to, subject, text, attach=None):
    ''' Dùng Gmail để gửi thông tin alert đi'''
    logger.debug('start send mail({0}, {1}, {2})'.format(to, subject, text))
    try:
        msg = MIMEMultipart()
        msg['From'] = gmail_user
        msg['To'] = to
        msg['Subject'] = subject
        msg.attach(MIMEText(text))
        if attach != None:
           part = MIMEBase('application', 'octet-stream')
           part.set_payload(open(attach, 'rb').read())
           Encoders.encode_base64(part)
           part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attach))
           msg.attach(part)
        mailServer = smtplib.SMTP("smtp.gmail.com", 587)
        mailServer.ehlo()
        mailServer.starttls()
        mailServer.ehlo()
        mailServer.login(gmail_user, gmail_pwd)
        mailServer.sendmail(gmail_user, to, msg.as_string())
        mailServer.close()
    except:
        traceback.print_exc()

class Mornitor():
    
    file_name = 'crawler_mornitor.data'
    must_report = []
    max_time = 5
    
    def __init__(self, host, port):
        self.connect(host, port)
        self.checkDataFile()
        
    def connect(self, host, port):
        try:
            self.connection = Connection(host, port)
        except:
            raise TypeError, 'Could not connect to mongo server'

    def getStartOfDay(self):
        dtpl = datetime.date.timetuple(datetime.date.today())
        return datetime.datetime(dtpl[0], dtpl[1], dtpl[2])
    
   
    def checkDataFile(self):
        logger.debug('start checkDataFile')
        try:
            if not os.path.isfile(os.path.join(current_dir, self.file_name)):
                fp = open(self.file_name, 'wb')
                pickle.dump({'lastaction': self.getStartOfDay(), 'data': {}}, fp)
                fp.close()
        except:
            logger.exception(str(sys.exc_info()[1]))
        logger.debug('finished checkDataFile')

    def readDataFile(self):
        logger.debug('start readDataFile')
        now = datetime.datetime.now()
        try:
            fp = open(self.file_name, 'rb')
            obj = pickle.load(fp)
            fp.close()
            if ((now.month > obj['lastaction'].month) or (now.day > obj['lastaction'].day)): 
                return {'lastaction': self.getStartOfDay(), 'data': {}} 
            return obj
        except:
            logger.exception(str(sys.exc_info()[1]))
            return {'lastaction': self.getStartOfDay(), 'data': {}}
    
    def writeDataFile(self, data):
        logger.debug('start writeDataFile')
        try:
            data['lastaction'] = datetime.datetime.now()
            fp = open(self.file_name, 'wb')
            pickle.dump(data, fp)
            fp.close()
        except:
            logger.exception(str(sys.exc_info()[1]))
    
    def makeReport(self):
        if len(self.must_report)>0:
            text = ''
            for item in self.must_report:
                text += '''Name: {0}\n'''.format(item['name'])
                for ev in item['err']:
                    text += '''\t{0}: {1}\n'''.format(ev['time'], ev['msg'])
                text += "\n"
            mail("giangnh@mana.vn", "Crawler mornitor error at {0}".format(datetime.datetime.now()), text)
        else:
            print 'There is not any critical error must to send email'
    
    def checkLog(self):
        logger.debug('start checkLog at {0}'.format(datetime.datetime.now()))
        try:
            db = self.connection['mongolog']
            collection = db['log']
            data = self.readDataFile()
            clist = data['data']
            rows = collection.find({'level': 'critical', 'time': {'$gte': data['lastaction']}}).sort('time', -1)
            if rows:
                for row in rows:
                    if not clist.has_key(row['name']): clist[row['name']] = {'err': [], 'c': 0, 'name': row['name']}
                    clist[row['name']]['c'] += 1
                    clist[row['name']]['err'].append({'msg': row['msg'], 'time': row['time']})
            pprint.pprint(data)
            for i in clist.keys():
                if clist[i]['c'] >= self.max_time: 
                    self.must_report.append(clist[i])
                    del clist[i]
            self.writeDataFile(data)
        except:
            logger.exception(str(sys.exc_info()[1]))
        logger.debug('finished checkLog at {0}'.format(datetime.datetime.now()))    
        
    def run(self):
        self.checkLog()
        self.makeReport()
        
        
if __name__ == '__main__':
    current_dir = os.path.dirname(__file__)
    print('start crawler mornitor at {0}'.format(datetime.datetime.now()))
    mornitor = Mornitor(MONGO_SERVER, MONGO_PORT)
    print '________MAX ERROR PER DAY = {0}_________'.format(mornitor.max_time)
    mornitor.run()
    print('finished crawler mornitor at {0}'.format(datetime.datetime.now()))
    os._exit(1)
